<template>
  <div class="seller" v-el:seller>
    <div class="seller-content">
      <div class="overview">
        <h2 class="title">{{ seller.name }}</h2>
        <div class="desc">
          <star class="star" :size="36" :score="seller.score"></star>
          <span class="text">({{ seller.ratingCount }})</span>
          <span class="text">月销{{ seller.sellCount }}单</span>
        </div>
        <div class="collection" @click="toggleFavorite" >
          <span class="icon icon-favorite" :class="{'selected' :favorite }"></span>
          <div class="text">{{ favoriteText }}</div>
        </div>
      </div>
      <ul class="remark">
        <li class="item">
          <p class="info">起送价</p>
          <p class="content">
            <span class="price">{{ seller.minPrice }}</span>元
          </p>
        </li>
        <li class="item">
          <p class="info">商家配送</p>
          <p class="content">
            <span class="price">{{ seller.deliveryPrice }}</span>元
          </p>
        </li>
        <li class="item">
          <p class="info">平均配送时间</p>
          <p class="content">
            <span class="price">{{ seller.deliveryTime }}</span>元
          </p>
        </li>
      </ul>
      <split-chunk></split-chunk>
      <div class="notice">
        <div class="content border-1px">
          <h2 class="title">公告与活动</h2>
          <p class="bulletin">{{ seller.bulletin }}</p>
        </div>

        <ul class="supports" v-if="seller.supports">
          <li class="supports-item" v-for="item in seller.supports">
            <span class="icon" :class="classMap[seller.supports[$index].type]"></span>
            <span class="text">{{ item.description}}</span>
          </li>
        </ul>
      </div>
      <split-chunk></split-chunk>
      <div class="pics">
        <h2 class="title">商家实景</h2>
        <div class="pic-wrapper" v-el:pic-wrapper>
          <ul class="pic-list" v-el:pic-list>
            <li class="pic-item" v-for="pic in seller.pics">
              <img :src="pic" width="120" height="90" />
            </li>
          </ul>
        </div>
      </div>
      <div class="seller-info">
        <h2 class="title">商家信息</h2>
        <ul>
          <li class="item" v-for="info in seller.infos">
            {{ info }}
          </li>
        </ul>
      </div>
    </div>
  </div>

</template>

<script>
  import BScroll from 'better-scroll'
  import Star from 'components/star/star'
  import { saveLocalStorage, readLocalStorage } from 'assets/scripts/store'
  import SplitChunk from 'components/split/split'
  
  export default {
    props: {
      seller: {
        type: Object
      }
    }, 
    data() {
      return {
        favorite: (() => {
          return readLocalStorage(this.seller.id, 'favorite', false)
        })()
      }
    },
    created() {
      this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
    },
    computed: {
      favoriteText() {
        return this.favorite ? '已收藏' : '收藏'
      }
    },
    watch: {
      'seller'() {
        this._initScroll()
        this._initPics()
      }
    },
    ready() {
      this._initScroll()
      this._initPics()
    },
    methods: {
      toggleFavorite(event) {
        if (!event._constructed) {
          return
        }
        this.favorite = !this.favorite
        saveLocalStorage(this.seller.id, 'favorite', this.favorite)
      },
      _initScroll() {
        if(!this.scroll) {
          this.$nextTick(() => {
            this.scroll = new BScroll(this.$els.seller, {
              click: true
            })
          })
        }
        else {
          this.scroll.refresh()
        }
      },
      _initPics() {
        if(this.seller.pics) {
          let picWidth = 120
          let rightMargin = 6
          let iW = (picWidth + rightMargin) * this.seller.pics.length

          this.$els.picList.style.width = iW + 'px'

          this.$nextTick(() => {
            if(!this.picScroll) {
              this.picScroll = new BScroll(this.$els.picWrapper, {
                click: true,
                scrollX: true,
                eventPassthrough: 'vertical'
              })
            }
            else {
              this.picScroll.refresh()
            }
          })
        }
      }
    },
    components: {
      Star,
      'split-chunk': SplitChunk
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import '../../assets/style/minxin.stylus'
  .seller
    position: absolute
    top: 180px
    left: 0
    bottom: 0
    width: 100%
    overflow: hidden
    h2.title
      font-size: 14px
      color: rgb(7, 17, 27)
      line-height: 14px
      margin-bottom: 8px
    .seller-content
      .overview
        position: relative
        padding: 18px 18px 0 18px
        .desc
          padding-bottom: 18px
          border-1px(rgba(7, 17, 27, .1))
          font-size: 0
          .star
            display: inline-block
            vertical-align: top
            height: 18px
            margin-right: 8px
          .text
            vertical-align: top
            margin-right: 12px
            line-height: 18px
            font-size: 10px
            color: rgb(77, 85, 93)
        .collection
          position: absolute
          right: 0
          top: 18px
          width: 50px
          text-align: center
          padding-right: 18px
          .icon-favorite
            font-size: 24px
            line-height: 24px
            color: #d4d6d9
            &.selected
              color: rgb(240, 20, 20)
          .text
            margin-top: 4px
            font-size: 10px
            line-height: 10px
            color: rgb(77, 85, 93)
      .remark
        display: flex
        flex-direction: row
        padding: 18px 0 
        .item
          flex: 1
          text-align: center
          border-right: 1px solid rgba(7, 17, 27, .1)
          &:last-child
            border: none
          .info
            margin-bottom: 4px
            font-size: 10px
            color: rgb(147, 153, 159)
            line-height: 10px
          .content
            font-size: 10px
            color: rgb(7, 17, 27)
            line-height: 24px
            .price
              font-size: 24px
      .notice
        padding: 18px 18px 0 18px
        .content
          padding-bottom: 18px
          border-1px(rgba(1, 17, 27, .1))
          .bulletin
            padding: 0 12px
            color: rgb(240, 20, 20)
            font-size: 12px
            line-height: 24px
        .supports
          .supports-item
            padding: 12px 16px
            font-size: 0
            border-1px(rgba(7, 17, 27, .1))
            &:last-child
              border-none()
          .icon
            vertical-align: top
            display: inline-block
            width: 16px
            height: 16px
            margin-right: 6px
            background-size: 16px 16px
            background-repeat: no-repeat
            &.decrease
              bg-image('decrease_4')
            &.discount
              bg-image('discount_4')
            &.guarantee
              bg-image('guarantee_4')
            &.invoice
              bg-image('invoice_4')
            &.special
              bg-image('special_4')
          .text
            vertical-align: top
            font-size: 12px
            color: rgb(7, 17, 27)
            line-height: 16px
      .pics
        padding: 18px
        h2.title
          margin-bottom: 12px
        .pic-wrapper
          width: 100%
          overflow: hidden
          .pic-list
            .pic-item
              display: inline-block
              width: 120px
              height: 90px
              margin-right: 6px
              &:last-child
                margin: 0
      .seller-info
        padding: 18px
        h2.title
          margin-bottom: 12px
        .item
          padding: 16px 12px
          font-size: 12px
          color: rgb(7, 17, 27)
          line-height: 16px
          border-1px(rgba(7, 17, 27, .1))
          &:last-child
            border-none()
</style>
